const puppeteer = require('puppeteer');
const fs = require('fs');

let totalBlogs = [];

async function main() {
  let totalBlogs = [];
  const url = 'https://www.zhihu.com/search?type=content&q=feign%20dubbo';

  const browser = await puppeteer.launch({headless: false});
  const page = (await browser.pages())[0];
  await page.setViewport({width: 1500, height: 0});
  await page.setExtraHTTPHeaders({
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36',
    'cookie': '_zap=ee58ab8e-5196-4cf5-b71d-caa85946f723; _xsrf=fjWMDQ3nfYVGDFxdPxNaqNOtrHurMhqu; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1607778476; d_c0="AFBfStPXVRKPTopINQ4ZEmWerKdeJ4CQZ3M=|1607778476"; capsion_ticket="2|1:0|10:1607778476|14:capsion_ticket|44:ZDEzMjc4N2I0YWZmNGRjY2JkODdiZDBmN2QyMTI1YzE=|d1617145d33b42b7fb52c14db173b6b0a101380bc84a0283aab283bd00548154"; SESSIONID=zd1FbtTTw9AXkUh3IFS6yhMrsUUg6vnkKkCAUebynXS; JOID=UVEUCkj_mr0Go2whK_UAaGp1sX47j_aESsESfXeIqfRWwjlDEFBaa1OpbiEpU3nwTSb892wgIqwOouQDHUUj-00=; osd=UV4VCk__lbwGpGwuKvUHaGV0sXk7gPeETcEdfHePqftXwj5DH1FabFOmbyEuU3bxTSH8-G0gJawBo-QEHUoi-0o=; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1607778493; KLBRSID=d1f07ca9b929274b65d830a00cbd719a|1607778493|1607778475',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'none',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1'
  });
  page.on('console', msg => {
    console.log(msg.text());
  });

  page.on('pageerror', msg => {
    console.log(msg);
  });


  await page.goto(url, {waitUntil: 'domcontentloaded'});
  await page.waitFor(3000);

  totalBlogs = await page.$$eval('#SearchMain > div > div > div > div > div', async (eles) => {
    let tmpBlogs = [];
    eles.forEach(async el => {
      let title = el.querySelector('div > div > h2 > a').innerText;
      let content = el.querySelector('div > div > h2 > a').href;
      let intro = el.querySelector('div > div > div > div.RichContent-inner > span').innerText;
      tmpBlogs.push({
        title: title,
        content: content,
        intro: intro,
        tab: 'tech',
        sub_tab: '思否',
        source: 'zhihu',
        author_id: '5f743a5cea022430046c0d7d',
        author_name: '思否',
        author_avatar: '/public/images/segmentfault.png'
      });
    });
    return tmpBlogs;
  });

  console.log(totalBlogs);
};


main();


